package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YiSaiServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "yisai")
public class YiSaiAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(YiSaiAdaptor.class);
	private static String HOST_CALLBACK_URL = BetweenProperties.CALLBACK_URL;//回调地址
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productid = orderBean.getProductCode();
		String mobile = orderBean.getPhoneNo();
		String transNo =orderBean.getIds();
		String back_url = HOST_CALLBACK_URL+"Yisai";
		log.info("between- 南宁易赛订单YiSaiService.order() 订购请求productid:" + productid+",mobile:"+mobile+",transNo:"+transNo+",back_url:"+back_url);
		String result = YiSaiServiceImpl.service.order(productid, mobile, transNo,back_url);
		log.info("between- 南宁易赛订单YiSaiService.order() 订购响应response<>:" + result);
		
	
		
	
		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("Result");
			

		if ("success".equals(code)) {
			orderBean.setMessage(json.getString("Remark"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
		} else {

			orderBean.setMessage(json.getString("Remark"));
			orderBean.setErrorMsg(json.getString("Remark"));
			orderBean.setStatus("8");
		}
			
	
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getIds();
		log.info("between -南宁易赛状态YiSaiService.orderStatus() 查询请求cstmOrderNo:" + cstmOrderNo);
		String result = YiSaiServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 南宁易赛状态YiSaiService.orderStatus() 查询响应response<> :" + result);

		
		JSONObject json = JSONObject.fromObject(result);
        String status = json.getString("Result");
        if ("success".equals(status)) {
            String code = json.getString("PayResult");

    		if ("2".equals(code)) {
    			orderBean.setMessage("充值成功");
    			orderBean.setErrorMsg("订购成功");
    			orderBean.setStatus("7");
    		} else if ("1".equals(code)) {
 
    		    orderBean.setMessage("订购中");
    			orderBean.setErrorMsg("订购中");
    			orderBean.setStatus("1");
    		} else {
   
    			orderBean.setMessage(json.getString("Remark"));
    			orderBean.setErrorMsg("订购失败");
    			orderBean.setStatus("8");
    		}
    			
		}else {
			orderBean.setMessage("订购中");
  			orderBean.setErrorMsg("订购中");
  			orderBean.setStatus("1");
		}
			
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
